package cn.kgc.service;

import cn.kgc.mapper.HmoldMapper;
import cn.kgc.pojo.Hmold;
import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-07 9:39
 */
@Service
public class ExcelServiceImpl implements ExcelService
{

    @Resource
    HmoldMapper hmoldMapper;

    @Override
    public int importExcel(MultipartFile excelFile)throws IOException, InvalidFormatException {
        List<Hmold> hmoldList= new ArrayList<Hmold>();
        InputStream excelStream= null;
        try {
            excelStream = excelFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取工作簿
        Workbook workbook= null;
        try {
            workbook = WorkbookFactory.create(excelStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }

        Logger logger=Logger.getLogger(this.getClass());
        logger.info("****sheetName:"+workbook.getSheetName(0));
        //获取工作表
        Sheet sheet=workbook.getSheetAt(0);
        //获取Excel总行
        int rowNum=sheet.getLastRowNum();
        logger.info("****rowNum:"+rowNum);
        //从内容开始
        if(rowNum>2){
            for (int i=2;i<rowNum;i++){
                Row row=sheet.getRow(i);
                Hmold hmold=new Hmold();

            hmold.setId((int) row.getCell(0).getNumericCellValue());
            hmold.setHousemold(row.getCell(1).getStringCellValue());

                hmoldList.add(hmold);
//                fileUploadDao.exportStudent()
            }
        }
        logger.info("****studentList:"+hmoldList);
//        int i = fileUploadDao.importExcel(studentList);
        int i = hmoldMapper.importExcel( hmoldList);
        logger.info("***returnNum:"+i);
        if(i==rowNum-2){
            return 1;
        }
        return 0;
    }
   /*
    @Override
    public int importExcel(MultipartFile excelFile) throws IOException, InvalidFormatException {
        List<student> studentList=new ArrayList<student>();
        InputStream excelStream=excelFile.getInputStream();
        //获取工作簿
        Workbook workbook= WorkbookFactory.create(excelStream);
        logger.info("****sheetName:"+workbook.getSheetName(0));
        //获取工作表
        Sheet sheet=workbook.getSheetAt(0);
        //获取Excel总行
        int rowNum=sheet.getLastRowNum();
        logger.info("****rowNum:"+rowNum);
        //从内容开始
        if(rowNum>2){
            for (int i=2;i<rowNum;i++){
                Row row=sheet.getRow(i);
                student student=new student();

                student.setId((int) row.getCell(0).getNumericCellValue());
                student.setName(row.getCell(1).getStringCellValue());
                student.setAge((int) row.getCell(2).getNumericCellValue());
                student.setSex(row.getCell(3).getStringCellValue());
                student.setBanji((int)row.getCell(4).getNumericCellValue());
                student.setMajor(row.getCell(5).getStringCellValue());
                student.setTime(row.getCell(6).getDateCellValue());

                studentList.add(student);
//                fileUploadDao.exportStudent()
            }
        }
        logger.info("****studentList:"+studentList);
        int i = fileUploadDao.importExcel(studentList);
        logger.info("***returnNum:"+i);
        if(i==rowNum-2){
            return 1;
        }
        return 0;
    }*/
}
